package com.njworkorder.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.Entity.*;
import com.njworkorder.HTSingleLogin.StringUtils;
import com.njworkorder.Mapper.PlanMeteringMapper;
import com.njworkorder.Service.PlanMonthlyAllocationService;
import com.njworkorder.Mapper.PlanMonthlyAllocationMapper;
import com.njworkorder.VO.PMAVo;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
* @author iron
* @description 针对表【plan_monthly_allocation】的数据库操作Service实现
* @createDate 2025-05-13 11:28:46
*/
@Service
public class PlanMonthlyAllocationServiceImpl extends MPJBaseServiceImpl<PlanMonthlyAllocationMapper, PlanMonthlyAllocation>
    implements PlanMonthlyAllocationService{

    private final PlanMeteringMapper  planMeteringMapper;
    public PlanMonthlyAllocationServiceImpl(PlanMeteringMapper planMeteringMapper){
        this.planMeteringMapper = planMeteringMapper;
    }

    @Async
    @Override
    public CompletableFuture<List<PlanMonthlyAllocation>> getList(String planId) {
        MPJLambdaWrapper<PlanMonthlyAllocation> wrapper = new MPJLambdaWrapper<PlanMonthlyAllocation>()
                .selectAll(PlanMonthlyAllocation.class)
                .leftJoin(Plan.class,Plan::getId,PlanMonthlyAllocation::getPlanId)
                .eq(Plan::getMeteringYear,LocalDate.now().getYear())
                .eq(PlanMonthlyAllocation::getPricingType,"总价");
        return CompletableFuture.completedFuture(selectJoinList(PlanMonthlyAllocation.class, wrapper));
    }

    @Async
    @Override
    public CompletableFuture<PMAVo> getRatioValue(String planId) {
        MPJLambdaWrapper<PlanMonthlyAllocation>  wrapper = new MPJLambdaWrapper<PlanMonthlyAllocation>()
                .selectAs("ROUND(SUM(output_value * (CAST(january_ratio AS DECIMAL(10,2)) / 100)), 2)", PMAVo::getJanuaryValue)
                .selectAs("ROUND(SUM(output_value * (CAST(february_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getFebruaryValue)
                .selectAs("ROUND(SUM(output_value * (CAST(march_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getMarchValue)
                .selectAs("ROUND(SUM(output_value * (CAST(april_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getAprilValue)
                .selectAs("ROUND(SUM(output_value * (CAST(may_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getMayValue)
                .selectAs("ROUND(SUM(output_value * (CAST(june_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getJuneValue)
                .selectAs("ROUND(SUM(output_value * (CAST(july_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getJulyValue)
                .selectAs("ROUND(SUM(output_value * (CAST(august_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getAugustValue)
                .selectAs("ROUND(SUM(output_value * (CAST(september_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getSeptemberValue)
                .selectAs("ROUND(SUM(output_value * (CAST(october_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getOctoberValue)
                .selectAs("ROUND(SUM(output_value * (CAST(november_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getNovemberValue)
                .selectAs("ROUND(SUM(output_value * (CAST(december_ratio AS DECIMAL(10,2)) / 100)), 2)",PMAVo::getDecemberValue)
                .eq(PlanMonthlyAllocation::getPricingType,"总价")
                .eq(PlanMonthlyAllocation::getPlanId,planId);

        return CompletableFuture.completedFuture(selectJoinOne(PMAVo.class, wrapper));
    }

    @Async
    @Override
    public CompletableFuture<List<PlanMetering>> getUseTotal(String planId) {


        MPJLambdaWrapper<PlanMetering> wrapper = new  MPJLambdaWrapper<PlanMetering>()

                .selectAs("SUM(t.unit_price * t2.num)",PlanMetering::getSpecialUnitPrice)
                .selectAs("MONTH(t2.is_date)",PlanMetering::getIsSpecial)

                .leftJoin(MeteringInventory.class,MeteringInventory::getId,PlanMetering::getMeteringInventoryId)
                .leftJoin(MeteringUse.class,MeteringUse::getProjectCode,MeteringInventory::getProjectNo)
                .eq(PlanMetering::getPlanId,planId)
                .eq(PlanMetering::getPricingType,1)
                .eq(MeteringUse::getPricingType,1)
                .groupBy("YEAR(t2.is_date)","MONTH(t2.is_date)");

        return CompletableFuture.completedFuture(planMeteringMapper.selectJoinList(PlanMetering.class, wrapper));
    }
}




